package com.xhj.myframe.myAQS;

import org.junit.Test;

import java.util.Random;
import java.util.concurrent.TimeUnit;
/** @author haijianxia pure_xhj@163.com @Date 2021-08-25 @Time 17:13 */
public class SynchronizedDemo {

  Object synLock = new Object();

  /** */
  @Test
  public void test01() {
    new Thread(
            () -> {
              Random random = new Random();
              int nextInt = random.nextInt(1000);
              //			try {TimeUnit.MICROSECONDS.sleep(55);} catch (Exception e) {e.printStackTrace();}
              synchronized (synLock) {
                System.out.println(Thread.currentThread().getName() + " notify...");
                synLock.notify();
              }
            },
            "BBB")
        .start();

    try {
      //			TimeUnit.MICROSECONDS.sleep(55);
      System.out.println("main...sleep...555");
    } catch (Exception e) {
      e.printStackTrace();
    }

    new Thread(
            () -> {
              synchronized (synLock) {
                System.out.println(Thread.currentThread().getName() + " come in and wait...");
                //				try {Thread.sleep(555);} catch (Exception e) {e.printStackTrace();}
                try {
                  TimeUnit.MICROSECONDS.sleep(1555);
                } catch (Exception e) {
                  e.printStackTrace();
                }
                try {
                  synLock.wait();
                } catch (InterruptedException e) {
                  e.printStackTrace();
                } finally {
                }
                System.out.println(Thread.currentThread().getName() + " is notified...");
              }
            },
            "AAA")
        .start();
  }

  static class MyThread extends Thread {
    @Override
    public void run() {
      System.out.println(".....");
    }
  }
}
